# -*- coding: utf-8 -*-
from django.db import models
from django.core.urlresolvers import reverse
import datetime
from django.contrib.auth.models import User
from cked.fields import RichTextField

FRONT_STATUS = [2, 3]

POST_TYPE_PAGE = 1
POST_TYPE_NOTE = 2

POST_STATUS_DRAFT = 1
POST_STATUS_PUBLISHED = 2
POST_STATUS_ARCHIVED = 3

class FrontManager(models.Manager):
    def get_query_set(self):
        default_queryset = super(FrontManager, self).get_query_set()
        return default_queryset.filter(status__in = FRONT_STATUS)

class Category(models.Model):
    title = models.CharField(max_length=255)
    url = models.CharField(max_length=100)
    description = models.TextField(blank=True)

    def __unicode__(self):
        return self.title
class Post(models.Model):
    """Blog posts and Pages"""
    POST_TYPE = (
        (1, u"Страница"),
        (2, u"Запись"),
    )

    STATUS_CHOICES = (
        (1, "Черновик"),
        (2, "Опубликован"),
        (3, "Архивирован")
    )

    title = models.CharField(max_length=255)
    url = models.CharField(max_length=100)
    category = models.ForeignKey(Category)
    html_content = RichTextField()
    owner = models.ForeignKey(User)
    status = models.IntegerField(choices=STATUS_CHOICES, default=1)
    type = models.IntegerField(choices=POST_TYPE, default=1)
    cdate = models.DateTimeField(default=datetime.datetime.now)
    mdate = models.DateTimeField(default=datetime.datetime.now)

    def __unicode__(self):
        return self.title
    class Meta:
        ordering = ['mdate']
    admin_objects = models.Manager()
    objects = FrontManager()

    def get_absolute_url(self):
        return reverse('posts.views.show', args=[str(self.url)])